package com.ruoyi.web.controller.business;

import java.util.List;
import java.util.Arrays;

import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.business.vo.HcxUserOpinionsVo;
import com.ruoyi.business.bo.HcxUserOpinionsQueryBo;
import com.ruoyi.business.bo.HcxUserOpinionsAddBo;
import com.ruoyi.business.bo.HcxUserOpinionsEditBo;
import com.ruoyi.business.service.IHcxUserOpinionsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 用户意见反馈Controller
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Api(value = "用户意见反馈控制器", tags = {"用户意见反馈管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/UserOpinions")
public class HcxUserOpinionsController extends BaseController {

    private final IHcxUserOpinionsService iHcxUserOpinionsService;

    /**
     * 查询用户意见反馈列表
     */
    @ApiOperation("查询用户意见反馈列表")
    @PreAuthorize("@ss.hasPermi('business:UserOpinions:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxUserOpinionsVo> list(@Validated HcxUserOpinionsQueryBo bo) {
        return iHcxUserOpinionsService.queryPageList(bo);
    }

    /**
     * 导出用户意见反馈列表
     */
    @ApiOperation("导出用户意见反馈列表")
    @PreAuthorize("@ss.hasPermi('business:UserOpinions:export')")
    @Log(title = "用户意见反馈", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxUserOpinionsQueryBo bo) {
        List<HcxUserOpinionsVo> list = iHcxUserOpinionsService.queryList(bo);
        ExcelUtil<HcxUserOpinionsVo> util = new ExcelUtil<>(HcxUserOpinionsVo.class);
        return util.exportExcel(list, "用户意见反馈");
    }

    /**
     * 获取用户意见反馈详细信息
     */
    @ApiOperation("获取用户意见反馈详细信息")
    @PreAuthorize("@ss.hasPermi('business:UserOpinions:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxUserOpinionsVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxUserOpinionsService.queryById(id));
    }

    /**
     * 新增用户意见反馈
     */
    @ApiOperation("新增用户意见反馈")
    @PreAuthorize("@ss.hasPermi('business:UserOpinions:add')")
    @Log(title = "用户意见反馈", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxUserOpinionsAddBo bo) {
        return toAjax(iHcxUserOpinionsService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户意见反馈
     */
    @ApiOperation("修改用户意见反馈")
    @PreAuthorize("@ss.hasPermi('business:UserOpinions:edit')")
    @Log(title = "用户意见反馈", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxUserOpinionsEditBo bo) {
        return toAjax(iHcxUserOpinionsService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户意见反馈
     */
    @ApiOperation("删除用户意见反馈")
    @PreAuthorize("@ss.hasPermi('business:UserOpinions:remove')")
    @Log(title = "用户意见反馈" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHcxUserOpinionsService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入用户意见反馈数据
     */
    @ApiOperation("导入用户意见反馈数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:UserOpinions:import')")
    @Log(title = "用户意见反馈" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxUserOpinionsVo> util = new ExcelUtil<>(HcxUserOpinionsVo.class);
        List<HcxUserOpinionsVo> hcxUserOpinionsList;
        try {
            hcxUserOpinionsList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxUserOpinionsService.importHcxUserOpinions(hcxUserOpinionsList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxUserOpinionsVo> util = new ExcelUtil<>(HcxUserOpinionsVo.class);
        return util.importTemplateExcel("用户意见反馈数据");
    }
}
